﻿using System.ComponentModel;
using System.Reflection;

namespace Hl.Core.Helper;

/// <summary>
/// 例子
/// </summary>
public enum ProgrammingLanguage
{
    /// <summary>
    /// 未知
    /// </summary>
    [Description("未知")]
    No = 0x00,
    /// <summary>
    /// C
    /// </summary>
    [Description("C语言")]
    C = 0x01,
    /// <summary>
    /// C#
    /// </summary>
    [Description("C#")]
    CSharp = 0x02,
    /// <summary>
    /// C++
    /// </summary>
    [Description("C++")]
    CPlusPlus = 0x3,
    /// <summary>
    /// JavaScript
    /// </summary>
    [Description("JavaScript")]
    JavaScript = 0x04,
    /// <summary>
    /// Html
    /// </summary>
    [Description("Html")]
    Html = 0x05,
    /// <summary>
    /// TypeScript
    /// </summary>
    [Description("TypeScript")]
    TypeScript = 0x06,
    /// <summary>
    /// CSS
    /// </summary>
    [Description("CSS")]
    Css = 0x07,
    /// <summary>
    /// Dockerfile
    /// </summary>
    [Description("Dockerfile")]
    Dockerfile = 0x08,
    /// <summary>
    /// Shell
    /// </summary>
    [Description("Shell")]
    Shell = 0x09,
    /// <summary>
    /// PowerShell
    /// </summary>
    [Description("PowerShell")]
    PowerShell = 0x0a,
    /// <summary>
    /// F#
    /// </summary>
    [Description("F#")]
    FSharp = 0x0b,
    /// <summary>
    /// Vue
    /// </summary>
    [Description("Vue")]
    Vue = 0x0c,
    /// <summary>
    /// VisualBasic
    /// </summary>
    [Description("Visual Basic")]
    VisualBasic = 0x0d,
    /// <summary>
    /// Python
    /// </summary>
    [Description("Python")]
    Python = 0x0e,
    /// <summary>
    /// Java
    /// </summary>
    [Description("JAVA")]
    Java = 0x0f,
    /// <summary>
    /// PHP
    /// </summary>
    [Description("PHP")]
    Php = 0x11,
    /// <summary>
    /// SQL
    /// </summary>
    [Description("SQL")]
    Sql = 0x12,
}

public static class EnumHelper
{

    #region 静态方法
    public static Dictionary<string, string> GetEnumDescription<T>() where T : Enum
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();

        FieldInfo[] fields = typeof(T).GetFields();

        foreach (FieldInfo field in fields)
        {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      

            if (field.FieldType.IsEnum)
            {

                object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);

                string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;

                dic.Add(field.Name, description);

            }
        }

        return dic;
    }

    /// <summary>        
    /// 获取对应的枚举描述        
    /// </summary>        
    public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>() where T : Enum
    {
        List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

        FieldInfo[] fields = typeof(T).GetFields();

        foreach (FieldInfo field in fields)
        {
            if (field.FieldType.IsEnum)
            {

                object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);

                string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;

                result.Add(new KeyValuePair<string, string>(field.Name, description));

            }

        }
        return result;
    }


    /// <summary>
    /// 获取枚举的 值和描述
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static List<KeyValuePair<int, string>> GetEnumValueDescriptionList<T>() where T : Enum
    {
        List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
        FieldInfo[] fields = typeof(T).GetFields();
        foreach (FieldInfo field in fields)
        {
            if (field.FieldType.IsEnum)
            {
                object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
                result.Add(new KeyValuePair<int, string>(Convert.ToInt32(field.GetValue(null)), description));
            }
        }

        return result;
    }

    public static string GetDescriptionByEnumName<T>(string name) where T : Enum
    {
        try
        {
            Dictionary<string, string> dic = GetEnumDescription<T>();
            string description = dic[name];
            return description;
        }
        catch (Exception ex)
        {
            return "";
        }
    }


    public static T Description2Enum<T>(string description) where T : Enum
    {
        var dic = GetEnumValueDescriptionList<T>();
        if (dic == null || dic.Count <= 0)
        {
            return default;
        }

        foreach (KeyValuePair<int, string> keyValuePair in dic)
        {
            var dValue = keyValuePair.Value;
            if (string.Equals(description,dValue,StringComparison.CurrentCultureIgnoreCase))
            {
                var dkey = keyValuePair.Key;
                return Value2Enum<T>(dkey);
            }
        }

        return default;
    }


    /// <summary>
    /// 值转枚举
    /// </summary>
    /// <typeparam name="T">枚举类型</typeparam>
    /// <param name="value">值</param>
    /// <returns></returns>
    public static T Value2Enum<T>(int value) where T : Enum
    {
        return (T)Enum.ToObject(typeof(T), value);
    }
    #endregion
}
